import { createSlice, PayloadAction } from '@reduxjs/toolkit'


interface  IState {
  value: number
  message: string
  direction?: 'up' | 'down' | 'left' | 'right'
}

// 传了类型更精确，不传只能类型推导出一个大概范围
const initialState : IState = {
  value: 0,
  message: 'Hello World'
}

const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state) => {
      state.value += 1
    },
    decrement: (state) => {
      state.value -= 1
    },
    // 
    incrementByAmount: (state, action: PayloadAction<number>) => {
      state.value += action.payload
    },
    setMessage: (state, { payload }: PayloadAction<string> ) => {
      state.message = payload
    }
  },
})

export const { increment, decrement, incrementByAmount, setMessage } = counterSlice.actions

export default counterSlice.reducer